package com.work.formaldehyde.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.umeng.analytics.MobclickAgent;
import com.work.formaldehyde.R;
import com.work.formaldehyde.homefragment.WodeFragment;
import com.work.formaldehyde.orderfragment.AllFragment;
import com.work.formaldehyde.orderfragment.DPJFragment;
import com.work.formaldehyde.orderfragment.DSHFragment;
import com.work.formaldehyde.orderfragment.DZFFragment;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class OrderActivity extends FragmentActivity implements View.OnClickListener {
    private ImageView comeback;
    private TabLayout order_tab;
    private ViewPager home_pager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApiUtils.ActivityIsLogin(this, 1);
        setContentView(R.layout.activity_order);
        comeback = findViewById(R.id.ordertitle_comeback);
        comeback.setOnClickListener(this);
        if (ApiUtils.isNetworkConnected(OrderActivity.this))
            init();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    //存放fragment
    List<Fragment> aryfrag;
    //创建ViewPager适配器
    private ViewPagerAdapter pagerAdapter;

    /**
     * 将fragment绑定到viewpager
     */
    public void SetViewPage() {
        aryfrag = new ArrayList<>();
        aryfrag.add(new AllFragment());
        aryfrag.add(new DZFFragment());
        aryfrag.add(new DSHFragment());
        aryfrag.add(new DPJFragment());
        //绑定点击事件
        home_pager.setOnPageChangeListener(new MyPagerChangeListener());
        //绑定适配器
        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), aryfrag);
        home_pager.setAdapter(pagerAdapter);
        home_pager.setCurrentItem(0);
        home_pager.setOffscreenPageLimit(4);
    }


    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            order_tab.getTabAt(position).select();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ApiUtils.popupWindows != null && ApiUtils.popupWindows.isShowing()) {
            return false;
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 初始加载
     */
    public void init() {
        int id = WodeFragment.sid;
        home_pager = findViewById(R.id.home_pager);
        order_tab = findViewById(R.id.order_tab);
        order_tab.addTab(order_tab.newTab().setText("全部"));
        order_tab.addTab(order_tab.newTab().setText("待支付"));
        order_tab.addTab(order_tab.newTab().setText("待收货"));
        order_tab.addTab(order_tab.newTab().setText("待评价"));
        order_tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                home_pager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        SetViewPage();
        switch (id) {
            case 0:
                order_tab.getTabAt(1).select();
                home_pager.setCurrentItem(1);
                break;
            case 1:
                order_tab.getTabAt(2).select();
                home_pager.setCurrentItem(2);
                break;

            case 2:
                order_tab.getTabAt(3).select();
                home_pager.setCurrentItem(3);
                break;
            case 3:
                order_tab.getTabAt(0).select();
                home_pager.setCurrentItem(0);
                break;
        }
        Map<String, Object> music = new HashMap<String, Object>();
        music.put("订单页面", "1");//自定义参数：音乐类型，值：流行
        MobclickAgent.onEventObject(OrderActivity.this, "order", music);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ordertitle_comeback:
                onBackPressed();
                break;
        }
    }


}
